package com.app.signing.view.room

import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.app.signing.R
import com.app.signing.data.DbManager
import com.app.signing.data.SpManager
import com.app.signing.view.base.BaseActivity
import com.app.signing.view.login.LoginStates
import com.app.signing.view.room.data.RoomSeatData
import com.app.signing.view.room.data.SeatType
import com.app.signing.view.room.view.OnSeatClickListener
import com.app.signing.view.signin.SignInActivity
import kotlinx.android.synthetic.main.activity_room212.bt_start_signing
import kotlinx.android.synthetic.main.activity_room212.view_room

/**
 * @author wangpeifeng
 * @date 2018/05/09 15:28
 */
abstract class BaseRoomActivity : BaseActivity() {

    protected var currentSeatData: RoomSeatData? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_room212
    }

    override fun initData() {
        // 查询是否有未结束的签到记录
        val signingId = SpManager.INSTANCE.readSigningDataId()
        if (signingId != -1L) {
            val seatData = DbManager.INSTANCE.findSeat(signingId)
            seatData?.let {
                if (it.roomId == getRoomId()) {
                    AlertDialog.Builder(this).setTitle("签到提醒").setMessage(
                        "有未结束的签到记录，请先结束签到"
                    ).setPositiveButton(
                        R.string.determine,
                        { _, _ ->
                            SignInActivity.start(this, it.roomId, it.seatId)
                            finish()
                        }
                    ).setNegativeButton(
                        R.string.cancel,
                        { _, _ -> finish() }).show()
                }
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        view_room.setOnSeatClickListener(onSeatClickListener())
        view_room.setData(buildData())
        bt_start_signing.setOnClickListener {
            if (currentSeatData == null) {
                showToast("请先选择座位")
            }
            currentSeatData?.let { SignInActivity.start(this, getRoomId(), it.id) }
        }
    }

    abstract fun getRoomId(): String

    abstract fun buildData(): List<List<RoomSeatData>>

    abstract fun onSeatClickListener(): OnSeatClickListener

    /**
     * 座位占座
     */
    fun signInSeat(data: RoomSeatData) {
        // 检测是否已经设置了签到范围
        if (!checkSignInRangeSetting()) {
            return
        }
        // 是否已选择了座位
        currentSeatData?.let {
            if (it.id != data.id) {
                showToast("已经选择了座位")
                return
            }
        }
        // 判断是常用座位模式，还是普通座位模式
        val statusId = SpManager.INSTANCE.readLoginStates()
        if (statusId == LoginStates.loginDefault) {
            when {
            // 普通座位模式下，点击座位后的状态改变
            // 空白座位，变红色
                data.type == SeatType.seatNo -> data.type = SeatType.seatRed
            // 红色座位，变白色
                data.type == SeatType.seatRed -> data.type = SeatType.seatNo
            // 黄色座位，提示已经为常用座位
                data.type == SeatType.seatYellow -> showToast("该座位已设置为常用座位,其他用户无法占用您的常用座位")
            }
        } else if (statusId == LoginStates.loginSpecial) {
            when {
            // 常用座位模式下，同上
                data.type == SeatType.seatNo -> data.type = SeatType.seatYellow
                data.type == SeatType.seatYellow -> data.type = SeatType.seatNo
                data.type == SeatType.seatRed -> data.type = SeatType.seatNo
            }
        }
        if (data.type == SeatType.seatNo) {
            currentSeatData = null
        } else {
            // 设置了座位后
            currentSeatData = data
            if (data.type == SeatType.seatRed) {
                showToast("占座成功")
            } else if (data.type == SeatType.seatYellow) {
                showToast("常用座位设置成功")
            }
        }
        // 界面上的状态更新
        view_room.updateSeatData(data)
    }

    protected fun checkSignInRangeSetting(): Boolean {
        val signingSetting = SpManager.INSTANCE.readSigningSetting()
        if (signingSetting.latitude == 0.0 || signingSetting.longitude == 0.0) {
            showToast("未设置签到范围，请先登录管理员设置签到范围")
            return false
        }
        return true
    }
}